<template>
  <div class="mnt-dialog form-container">
    <el-form ref="form" :model="formModel" label-width="160px" :rules="rules">
      <el-row style="text-align: center">
        <el-col :span="12">
          <el-form-item label="缺陷分类编码:" prop="classifyIssueCode">
            <el-select v-model="formModel.classifyIssueCode" clearable filterable>
              <el-option
                v-for="item in classifyIssueOptions"
                :key="item.classifyIssueCode"
                :label="item.classifyIssueCode+'-'+item.classifyIssueName"
                :value="item.classifyIssueCode"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="缺陷编码:" prop="issueCode">
            <el-input v-model="formModel.issueCode" :readonly="editReadOnly" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="缺陷描述:" prop="issueName">
            <el-input v-model="formModel.issueName" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="缺陷等级:" prop="modulesIssueGradeKey">
            <hey-select v-model="formModel.modulesIssueGradeKey" clcd="issue_grade" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row class="submit-area">
      <el-col :span="24">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="handleSubmit()">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>

export default {
  name: 'IssueMnt',
  props: ['pageType', 'editData'],
  data() {
    return {
      classifyIssueOptions: [],
      editReadOnly: false,
      formModel: {
        issueId: '',
        classifyIssueCode: '',
        issueCode: '',
        issueName: '',
        modulesIssueGradeKey: ''
      },
      // 老数据
      oldFormModel: {
      },
      rules: {}
    }
  },
  computed: {},
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.$form = this.$refs.form
      const { pageType, editData, formModel } = this
      if (pageType !== 'add') {
        this.assignData(editData, formModel)
        this.oldFormModel = JSON.parse(JSON.stringify(this.formModel))
        this.editReadOnly = true
      }
      const param = {}
      this.getClassifyIssueOptional(param)
      this.initRules()
    },
    initRules() {
      const REQUIRED_MSG = this.CONST.REQUIRED_MSG
      this.rules = {
        issueCode: this.HEY_VALIDATOR({ type: ['ISSUE_CODE'], required: true }),
        issueName: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        modulesIssueGradeKey: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }]
      }
      this.$nextTick(_ => {
        this.$form.clearValidate()
      })
    },
    /** 查询缺陷分类列表 */
    getClassifyIssueOptional(param) {
      this.get('/classifyIssue/listClassifyIssue', param).then(res => {
        this.classifyIssueOptions = res
      })
    },
    // 提交新增或修改
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.formModel.issueId !== '') {
            // 判断是否修改
            if (JSON.stringify(this.formModel) !== JSON.stringify(this.oldFormModel)) {
              const tip = '修改成功！'
              const url = '/issue/updateIssue'
              this.post(url, this.formModel).then(res => {
                this.$info(tip, () => {
                  this.$emit('mntSuc', res)
                  this.closeDialog()
                })
              })
            } else {
              this.$emit('mntSuc')
              this.closeDialog()
            }
          } else {
            const url = '/issue/saveIssue'
            this.post(url, this.formModel).then(res => {
              const tip = '新增成功！'
              this.$info(tip, () => {
                this.$emit('mntSuc', res)
                this.closeDialog()
              })
            })
          }
        }
      })
    },
    closeDialog() {
      this.$parent.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
